<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">商户管理</a></li>
        <li class="active">商户管理</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="selectForm" class="form-inline" th:action="@{/merch/list}" role="form">
            <div class="form-group">
                <div class="form-group">
                    <label class="control-label">商户名称：</label>
                    <div class="control-inline">
                        <input type="text" name="merchName" class="form-control" placeholder="可模糊查询" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">商户编号：</label>
                    <div class="control-inline">
                        <input type="text" name="mchId" class="form-control width-90" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">手机号：</label>
                    <div class="control-inline">
                        <input type="text" name="merchPhone" class="form-control width-90" />
                    </div>
                </div>

                <shiro:hasPermission name="merch:list">
                    <div class="btn-group btn-group-sm" style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                        <button type="reset" id="btn_reset" class="btn btn-warning">重置</button>
                    </div>
                </shiro:hasPermission>
            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="toolbar btn-group">
            <shiro:hasPermission name="merch:add">
                <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" data-target="#merchModal">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="merch:batchDelete">
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
                </button>
            </shiro:hasPermission>
        </div>


        <div class="wrapper-content">
            <div class="ibox-content animated fadeInRight">
                <div class="jqGrid_wrapper">
                    <table id="table_list"></table>
                    <div id="pager_list"></div>
                </div>
            </div>
        </div>

    </div>
</section>

<!--代理商新增模态框-->
<div id="merchModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">新增商户</h4>
            </div>
            <div class="modal-body">
                <div>
                    <form class="form-horizontal" id="merchForm" method = 'post' th:action="@{/merch/add}">
                        <div class="box-body">
                            <div class="form-group required">
                                <label for="merchName" class="col-sm-3 control-label form-required">商户名称 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchName" id="merchName" require="必填项"  placeholder="">
                                </div>
                            </div>
                            <div class="form-group required">
                                <label for="merchAccount" class="col-sm-3 control-label">登录账号 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchAccount" id="merchAccount" require="必填项"  placeholder="">
                                </div>
                            </div>
                            <div class="form-group required">
                                <label for="merchPwd" class="col-sm-3 control-label">登录密码 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchPwd" id="merchPwd" require="必填项"  placeholder="">
                                </div>
                            </div>
                            <div class="form-group required">
                                <label for="merchCompany" class="col-sm-3 control-label">公司名称 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchCompany" id="merchCompany" require="必填项" placeholder="">
                                </div>
                            </div>

                            <div class="form-group required">
                                <label for="merchPhone" class="col-sm-3 control-label">联系电话 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchPhone" id="merchPhone" require="必填项" placeholder="">
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="merchMail" class="col-sm-3 control-label">邮箱 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchMail" id="merchMail" placeholder="">
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="merchAddress" class="col-sm-3 control-label">联系地址 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="merchAddress" id="merchAddress" placeholder="">
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="merchRemark" class="col-sm-3 control-label">备注 :</label>
                                <div class="col-sm-8">
                                    <textarea placeholder="" class="form-control" rows="3" name="merchRemark" id="merchRemark"></textarea>
                                </div>
                            </div>

                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <div class="pull-right">
                                <button type="button" id="saveMerch" class="btn btn-info">确定</button>
                                <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                            </div>
                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 商户详情模态框 -->
<div id="merchDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">商户信息</h4>
            </div>
            <div class="modal-body">
                <div id="merchOpenWindow">
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<!-- 商户费率模态框 -->
<div id="merchRateModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">设置商户费率</h4>
            </div>
            <div class="modal-body">
                <div id="merchRateOpenWindow">
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<script type="text/javascript">
    $(document).ready(function () {
        var editFlag = "[[${@perms.hasPerm('merch:edit')}]]";
        var deleteFlag = "[[${@perms.hasPerm('merch:delete')}]]";

        var dataGrid = new DataGrid({

            dataGrid: $('#table_list'),
            searchForm: $('#selectForm'),
            dataGridPage: $('#pager_list'),
            multiselect: true,
            multiboxonly: true,
            rownumbers: true,
            rownumWidth: 60,
            colModel: [
                {
                    header: 'ID',
                    name: 'id',
                    index: 'id',
                    editable: false,
                    width: 20,
                    sortable: false,
                    search: false
                },
                {
                    header: '商户名称',
                    name: 'merchName',
                    index: 'merchName',
                    editable: false,
                    width: 70,
                    sortable: false,
                    search: false
                },
                {
                    header: '商户号',
                    name: 'merchMerchantNo',
                    index: 'merchMerchantNo',
                    width: 45,
                    sortable: false
                },
                {
                    header: '登录账号',
                    name: 'merchAccount',
                    index: 'merchAccount',
                    width: 35,
                    sortable: false
                },
                {
                    header: '手机号',
                    name: 'merchPhone',
                    index: 'merchPhone',
                    sortable: false,
                    width: 35
                },
                {
                    header: '状态',
                    name: 'merchStatus',
                    index: 'merchStatus',
                    sortable: false,
                    width: 30,
                    formatter: function (cellvalue, options, rowObject) {
                        if (1==cellvalue) {
                            return "启用";
                        } else if (0==cellvalue) {
                            return "停用";
                        }
                    }
                },
                {
                    header: '联系邮箱',
                    name: 'merchMail',
                    index: 'merchMail',
                    editable: true,
                    width: 30,
                    sortable: false
                },
                {
                    header: '联系地址',
                    name: 'merchAddress',
                    index: 'merchAddress',
                    editable: true,
                    width: 30,
                    sortable: false
                },
                {
                    header: '备注',
                    name: 'merchRemark',
                    index: 'merchRemark',
                    editable: true,
                    width: 50,
                    sortable: false
                },
                {
                    header: '操作',
                    name: 'operate',
                    index: 'operate',
                    width: 80,
                    align: 'center',
                    sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        /*var edit = editFlag == "true" ? '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editMerch(' + rowObject.id + ')">编辑</a>' : '';
                        var del = deleteFlag == "true" ? '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteMerch(' + rowObject.id + ')">删除</a>' : '';*/
                        var edit = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editMerch(' + rowObject.id + ')">编辑</a>' ;
                        var del = '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteMerch(' + rowObject.id + ')">删除</a>' ;
                        var rate =  '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="updaterate(' + rowObject.id + ')">设置费率</a>' ;
                        return edit + del + rate;
                    }
                }
            ]
            //gridComplete:renderSwitch
        });

        //加载表格
        $("#btn_query").click(function () {
            $("#table_list").jqGrid('setGridParam', {
                datatype: 'json',
                postData: {
                    merchName: $('input[name=merchName]').val(),
                    merchPhone: $('input[name=merchPhone]').val(),
                    merchMerchantNo: $('input[name=mchId]').val()
                }, // 发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入
        });

        //新增-保存
        $("#saveMerch").click(function(){
            if(doValidForm(merchForm)){
                Core.mask("#saveMerch");
                Core.postAjax(ctxPath + "/merch/add",$("#merchForm").serialize(),function (data) {
                    Core.unmask("#saveMerch");
                    if(data.code==200){
                        $("#merchModal").modal("hide");
                        $("#merchForm")[0].reset();
                        Core.refreshJqTable("#table_list");
                    }
                    layer.msg(data.msg);
                })
            };
        });

        var roleId="";
        var merchIdStr;//批量
        //批量删除
        $("#btn_batch_delete").click(function(){
            var checkedRows= Core.selectJqMutiData("#table_list");
            if(checkedRows!=false){
                Core.confirm("确定删除选中的"+checkedRows.length+"条记录？",function () {
                    merchIdStr="";
                    $.each(checkedRows, function (i, item) {
                        var rowData = $("#table_list").jqGrid('getRowData', checkedRows[i]);
                        merchIdStr+=(rowData.id+",");
                    });
                    merchIdStr=merchIdStr.substring(0,merchIdStr.length-1);
                    Core.postAjax(ctxPath + "/merch/batch/delete",{"merchIdStr":merchIdStr},function(data){
                        if(data.code==200){
                            Core.refreshJqTable("#table_list");
                        }
                        layer.msg(data.msg);
                    },"get")
                })
            }
        });

    });

    /*删除*/
    function deleteMerch(merchId) {
        Core.confirm("确定删除该商户？",function () {
            Core.postAjax(ctxPath + "/merch/delete", {"merchId": merchId}, function (data) {
                if (data.code == 200) {
                    Core.refreshJqTable("#table_list");
                }
                layer.msg(data.msg);
            }, "get")
        })
    }


    function updaterate(merchId){
        Core.load("#merchRateOpenWindow", ctxPath + "/merch/merchrate?&merchId=" + merchId, function () {
            $("#merchRateModal").modal("show");
        }, 2);
    }

    /*编辑*/
    function editMerch(merchId) {
        Core.load("#merchOpenWindow", ctxPath + "/merch/edit?&merchId=" + merchId, function () {
            $("#merchDetailModal").modal("show");
        }, 2);
    }

</script>
